﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System.Web.UI;
using ESF.General.MVP;
using System;
using ESF.General;
using System.Web.UI.WebControls;
using System.Web;

#endregion


namespace ESF.Web.Pages
{
	public static class UserControlHelper
	{
		#region Render Button
		
		public static void RenderButton (HtmlTextWriter pWriter, WebControl pContainer,
			string pID, string pLabel, string pToolTip, string pOnClientClick, short pTabIndex)
		{
			RenderButton (pWriter, pContainer,
				pID, pLabel, pToolTip, pOnClientClick,
				false, string.Empty, pTabIndex);
		}
		

		public static void RenderButton (HtmlTextWriter pWriter, WebControl pContainer,
			string pID, string pLabel, string pToolTip, string pOnClientClick,
			bool pCausesValidation, string pValidationGroup, short pTabIndex)
		{
			RenderButton (pWriter, pContainer,
				pID, pLabel, pToolTip, pOnClientClick,
				pCausesValidation, pValidationGroup, pTabIndex, true);
		}
		
		
		public static void RenderButtonNP (HtmlTextWriter pWriter, WebControl pContainer,
			string pID, string pLabel, string pToolTip, string pOnClientClick, short pTabIndex)
		{
			RenderButton (pWriter, pContainer,
				pID, pLabel, pToolTip, pOnClientClick,
				false, null, pTabIndex, false);
		}


		public static void RenderButton (HtmlTextWriter pWriter, WebControl pContainer,
			string pID, string pLabel, string pToolTip, string pOnClientClick,
			bool pCausesValidation, string pValidationGroup, short pTabIndex, bool pDoPostBack)
		{
			PostBackOptions postOpt;
			
			Button btnButton;
			
			
			postOpt = new PostBackOptions (pContainer);
			postOpt.Argument = pID;
			postOpt.PerformValidation = pCausesValidation;
			postOpt.ValidationGroup = pValidationGroup;
			postOpt.RequiresJavaScriptProtocol = true;
			
			btnButton = new Button ();
			btnButton.ID = pID;
			btnButton.ToolTip = pToolTip;
			btnButton.Text = pLabel;
			
			
			if (pTabIndex > 0)
			{
				btnButton.TabIndex = pTabIndex;
			}
				
			if (!string.IsNullOrEmpty (pOnClientClick))
			{
				btnButton.Attributes ["onclick"] = pOnClientClick;
			}
				
			if (pDoPostBack)
			{
				btnButton.Attributes.Add ("onclick", pContainer.Page.ClientScript.GetPostBackEventReference (postOpt) + ";");
			}
				
			btnButton.RenderControl (pWriter);
				
			pContainer.Controls.Add (btnButton);
		}
		
		#endregion
		
		
		#region Render Form

		public static void RenderFormHeader
		(
			HtmlTextWriter pWriter, 
			string pTitle,
			string pSubTitle,
			bool pShowClose,
			string pFormDivID
		)
		{
				pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "header");
			pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
						
					pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "title");
				pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
				            
				    pWriter.RenderBeginTag (HtmlTextWriterTag.Span);
						pWriter.Write (pTitle);
					pWriter.RenderEndTag();

						pWriter.AddAttribute(HtmlTextWriterAttribute.Class, "close");
						pWriter.AddAttribute(HtmlTextWriterAttribute.Onclick, "HideOverByID ('" + pFormDivID + "');");
					pWriter.RenderBeginTag(HtmlTextWriterTag.Div);
					pWriter.RenderEndTag();

				pWriter.RenderEndTag ();
						
					pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "subtitle");
				pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
				            
				    pWriter.Write (pSubTitle);
				pWriter.RenderEndTag ();
									
			pWriter.RenderEndTag ();
		}
		
		#endregion
		
		
		#region Render Field

		#region Generic
		// TODO: Globalizar mensajes
		private static void RenderField 
		(
			HtmlTextWriter pWriter, 
			Control pField,
			string pLabel,
			string pInstructions,
			RequiredFieldValidator pRequiredValidator,
			RegularExpressionValidator pRegularValidator,
			CompareValidator pCompareValidator,
			string pValidationGroup,
			bool pDoAutoPostBack
		)
		{
			PostBackOptions postOpt;
			
		
			if (pField == null) { throw new ArgumentNullException ("El control no ha sido proporcionado: pField"); }
			
			if (pField.Parent == null) { throw new Exception ("El control no ha sido vinculado a un control"); }
		
			#region PostBack
			
			postOpt = new PostBackOptions (pField.Parent);
			postOpt.Argument = pField.ID;
			postOpt.PerformValidation = false;
			postOpt.AutoPostBack = pDoAutoPostBack;
			postOpt.ValidationGroup = pValidationGroup;
			
			#endregion
			
		
				pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "divField");
			pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
				
					pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "label");
				pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
				
				pWriter.RenderBeginTag (HtmlTextWriterTag.Span);
					pWriter.Write (pLabel);
				pWriter.RenderEndTag ();
					
					if (pRequiredValidator != null)
					{
						if (pRequiredValidator.ControlToValidate != pField.ID)
						{
							throw new Exception (string.Format ("El validador para Requerido no esta vinculado al campo {0}", pLabel));
						}
						
						if (pRequiredValidator.Parent != pField.Parent)
						{
							throw new Exception (string.Format ("El validador para Requerido de {0} no esta vinculado", pLabel));
						}
					
						pRequiredValidator.ID = "rfv" + pField.ID; 
						pRequiredValidator.Text = "&nbsp;*";
						pRequiredValidator.ControlToValidate = pField.ID;
						pRequiredValidator.SetFocusOnError = true;
						pRequiredValidator.Display = ValidatorDisplay.Dynamic;
						pRequiredValidator.ValidationGroup = pValidationGroup;
						
						if (pField is ListBox || pField is DropDownList)
						{
							pRequiredValidator.ErrorMessage = "Seleccione " + pLabel;
						}
						else
						{
							pRequiredValidator.ErrorMessage = "Diligencie " + pLabel;
						}
						
						pRequiredValidator.RenderControl (pWriter);
					}
					
					if (pRegularValidator != null)
					{
						if (pRegularValidator.ControlToValidate != pField.ID)
						{
							throw new Exception (string.Format ("El validador para Expresión Regular no esta vinculado al campo {0}", pLabel));
						}
						
						if (pRegularValidator.Parent != pField.Parent)
						{
							throw new Exception (string.Format ("El validador para Expresión Regular de {0} no esta vinculado", pLabel));
						}
						
						if (string.IsNullOrEmpty (pRegularValidator.ValidationExpression))
						{
							throw new Exception (string.Format ("No esta definida la expresion de validación del campo: {0}", pLabel));
						}
					
						pRegularValidator.ID = "rgv" + pField.ID;
						pRegularValidator.Text = "&nbsp;*";
						pRegularValidator.ControlToValidate = pField.ID;
						pRegularValidator.SetFocusOnError = true;
						pRegularValidator.Display = ValidatorDisplay.Dynamic;
						pRegularValidator.ValidationGroup = pValidationGroup;
						pRegularValidator.ErrorMessage = "Revise el formato para " + pLabel;
						
						pRegularValidator.RenderControl (pWriter);
					}
					
					if (pCompareValidator != null)
					{
						if (pCompareValidator.ControlToValidate != pField.ID)
						{
							throw new Exception (string.Format ("El validador para Comparación no esta vinculado al campo {0}", pLabel));
						}
						
						if (pCompareValidator.Parent != pField.Parent)
						{
							throw new Exception (string.Format ("El validador para Comparación de {0} no esta vinculado", pLabel));
						}
						
						if (string.IsNullOrEmpty (pCompareValidator.ControlToCompare))
						{
							throw new Exception (string.Format ("No esta definido el campo con el cual comparar a: {0}", pLabel));
						}
						
						pCompareValidator.ID = "cmv" + pField.ID;
						pCompareValidator.Text = "&nbsp;*";
						pCompareValidator.ControlToValidate = pField.ID;
						pCompareValidator.SetFocusOnError = true;
						pCompareValidator.Display = ValidatorDisplay.Dynamic;
						pCompareValidator.ValidationGroup = pValidationGroup;
						pCompareValidator.ErrorMessage = "El valor no coincide para " + pLabel;
						
						pCompareValidator.RenderControl (pWriter);
					}
				
				pWriter.RenderEndTag ();
					
					pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "field");
				pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
					
					if (pField is DropDownList || pField is ListBox)
					{
						pWriter.AddAttribute (HtmlTextWriterAttribute.Onchange, pField.Parent.Page.ClientScript.GetPostBackEventReference (postOpt));
					}
					else if (pField is HyperLink)
					{
						postOpt.RequiresJavaScriptProtocol = true;
						
						pWriter.AddAttribute (HtmlTextWriterAttribute.Src, pField.Parent.Page.ClientScript.GetPostBackEventReference (postOpt));
					}
					else if (pField is Button || pField is LinkButton)
					{
						pWriter.AddAttribute (HtmlTextWriterAttribute.Onclick, pField.Parent.Page.ClientScript.GetPostBackEventReference (postOpt));
				    }
				    
				    pField.RenderControl (pWriter);
				    
				    if (!string.IsNullOrEmpty (pInstructions))
					{
							pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "instructions");
						pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
							pWriter.Write (pInstructions);
						pWriter.RenderEndTag ();
					}
					
				pWriter.RenderEndTag ();
				
			pWriter.RenderEndTag ();
		}
		
		#endregion
				
		#region Generic Variations
		
		public static void RenderField 
		(
			HtmlTextWriter pWriter, 
			Control pField,
			string pLabel,
			string pInstructions
		)
		{
			RenderField (pWriter, pField, pLabel, pInstructions, null);
		}
		
		
		public static void RenderField 
		(
			HtmlTextWriter pWriter, 
			Control pField,
			string pLabel,
			string pInstructions,
			RequiredFieldValidator pRequiredValidator
		)
		{
			RenderField (pWriter, pField, pLabel, pInstructions, pRequiredValidator, null);
		}
		
		
		public static void RenderField 
		(
			HtmlTextWriter pWriter, 
			Control pField,
			string pLabel,
			string pInstructions,
			RequiredFieldValidator pRequiredValidator,
			string pValidationGroup
		)
		{
			RenderField (pWriter, pField, pLabel, pInstructions, pRequiredValidator, null, null, pValidationGroup, false);
		}
		
		#endregion
		
		#region TextBox
		
		public static void RenderField 
		(
			HtmlTextWriter pWriter, 
			TextBox pField,
			string pLabel,
			string pInstructions,
			RequiredFieldValidator pRequiredValidator,
			RegularExpressionValidator pRegularValidator,
			string pValidationGroup
		)
		{
			RenderField (pWriter, pField, pLabel, pInstructions, pRequiredValidator,
				pRegularValidator, null, pValidationGroup);
		}
		
		
		public static void RenderField 
		(
			HtmlTextWriter pWriter, 
			TextBox pField,
			string pLabel,
			string pInstructions,
			RequiredFieldValidator pRequiredValidator,
			RegularExpressionValidator pRegularValidator,
			CompareValidator pCompareValidator,
			string pValidationGroup
		)
		{
			RenderField (pWriter, pField, pLabel, pInstructions, pRequiredValidator,
				pRegularValidator, pCompareValidator, pValidationGroup, false);
		}
		
		#endregion
		
		#region DropDownList
		
		private static void RenderField 
		(
			HtmlTextWriter pWriter, 
			DropDownList pField,
			string pID, string pLabel,
			string pInstructions,
			RequiredFieldValidator pRequiredValidator,
			string pValidationGroup,
			bool pDoAutoPostBack
		)
		{
			RenderField (pWriter, pField, pLabel, pInstructions, pRequiredValidator, null, null, pValidationGroup, pDoAutoPostBack);
		}
		
		#endregion
				
		#endregion
		
		
		#region Render Start Operations
		
		public static void StartRenderOperations (HtmlTextWriter pWriter)
		{
				pWriter.AddAttribute (HtmlTextWriterAttribute.Class, "divOperations");
			pWriter.RenderBeginTag (HtmlTextWriterTag.Div);
		}
		
		#endregion
	}		
}
